# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=zed
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.187.8
pkgrel=1
pkgdesc="A high-performance, multiplayer code editor (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64') # clangarm64 crashes at startup https://github.com/kvark/blade/issues/222
url='https://zed.dev'
msys2_repository_url='https://github.com/zed-industries/zed'
msys2_documentation_url='https://zed.dev/docs'
license=('spdx:AGPL-3.0-or-later AND Apache-2.0 AND GPL-3.0-or-later')
install=zed-${MSYSTEM}.install
msys2_references=(
  'anitya: 373275'
  'archlinux: zed'
  'aur'
  'gentoo: app-editors/zed'
)
depends=("${MINGW_PACKAGE_PREFIX}-libgit2"
         "${MINGW_PACKAGE_PREFIX}-nodejs"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-protobuf"
             #"${MINGW_PACKAGE_PREFIX}-sqlite3"
             "${MINGW_PACKAGE_PREFIX}-cargo-about"
             "${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-mdbook"
             "${MINGW_PACKAGE_PREFIX}-rust-bindgen"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             'git')
optdepends=("${MINGW_PACKAGE_PREFIX}-clang-tools-extra: for a better C/C++ language support"
            "${MINGW_PACKAGE_PREFIX}-eslint: for a better JavaScript language support"
            "${MINGW_PACKAGE_PREFIX}-ollama: provides LLMs for assistance panel"
            "${MINGW_PACKAGE_PREFIX}-pyright: for a better Python language support"
            "${MINGW_PACKAGE_PREFIX}-rust-analyzer: for a better Rust language support"
            'git: for a better Git integration')
source=("git+${msys2_repository_url}.git#tag=v${pkgver}"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.15+zstd.1.5.7/download"
        "zstd-sys-remove-statik.patch")
sha256sums=('dc76ffaaa74343e49ab79f876af4f74b799e789ec28e147cfa9da2acc4a20e5e'
            'eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b')

prepare() {
  cd "${_realname}"
  rm rust-toolchain.toml

  # link system deps dynamically
  patch -d "../zstd-sys-2.0.15+zstd.1.5.7" -i "../zstd-sys-remove-statik.patch"
  # use patched *-sys crates
  sed -i '/\[patch\.crates-io\]/a zstd-sys = { path = "../zstd-sys-2.0.15+zstd.1.5.7" }' Cargo.toml

  # change git config so `pet*` crates would be fetched without error. requires Windows registry
  # changes, see
  # https://github.com/zed-industries/zed/blob/main/docs/src/development/windows.md#build-fails-path-too-long
  # for details
  if test true != "$(git config core.longpaths)"; then
    git config --global core.longpaths true
  fi

  cargo update -p zstd-sys
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
  # generate license for whole Zed contents
  sh script/generate-licenses
}

build() {
  cd "${_realname}"

  # our protoc fails
  #export PROTOC="${MINGW_PREFIX}/bin/protoc.exe"
  #export PROTOC_INCLUDE="${MINGW_PREFIX}/include"
  export OPENSSL_NO_VENDOR=1
  export LIBGIT2_NO_VENDOR=1
  export PKG_CONFIG_ALL_DYNAMIC=1
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  # see https://github.com/zed-industries/zed/issues/17695
  #export LIBSQLITE3_SYS_USE_PKG_CONFIG=1
  export ZED_UPDATE_EXPLANATION='Updates are handled by pacman'
  export CARGO_PROFILE_RELEASE_DEBUG=0
  export RELEASE_VERSION="${pkgver} (Rev${pkgrel}, Built by MSYS2 project)"
  # should be synced with .cargo/config.toml. use lld linker to fix keyboard shortcuts
  # https://github.com/msys2/MINGW-packages/issues/22071
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static} -C link-arg=-fuse-ld=lld
    -C symbol-mangling-version=v0 --cfg tokio_unstable --cfg windows_slim_errors"

  cargo build --release --frozen -p zed -p cli

  cd docs
  mdbook build
}

check() {
  cd "${_realname}"

  cargo test --release --frozen -p zed -p cli
}

package_zed() {
  install -Dm755 "${_realname}/target/release/cli" "${pkgdir}${MINGW_PREFIX}/bin/zeditor"
  install -Dm755 "${_realname}/target/release/zed" "${pkgdir}${MINGW_PREFIX}/lib/zed/zed-editor"
  install -Dm644 "${_realname}/assets/licenses.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}

package_zed-docs() {
  pkgdesc+=" (Documentation)"
  depends=()
  optdepends=()

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/" "${_realname}"/docs/book/*
}

# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
